﻿using System;
using System.Drawing;
using System.Windows.Forms;


namespace StickTops
{

    /// <summary>
    /// Starter for stick top
    /// </summary>
    public partial class LoginForm : Form
    {

        private Point LastCursorPosition;
        private bool IsMouseDown = false;
        private Bitmap MinimizePic;
        private Bitmap ClosePic;
        private Bitmap MinimizePicOver;
        private Bitmap ClosePicOver;
        public bool IsClosed = false;
        private RegisterForm registerForm;
        

        public LoginForm()
        {
            InitializeComponent();

            MinimizePic = new Bitmap(Properties.Resources.bar_min);
            ClosePic = new Bitmap(Properties.Resources.bar_close);
            MinimizePicOver = new Bitmap(Properties.Resources.bar_min_over);
            ClosePicOver = new Bitmap(Properties.Resources.bar_close_over);

            lblVer.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(4);
            txtUsername.Text = Properties.Settings.Default.Username;
            txtPassword.Text = Properties.Settings.Default.Password;
            checkBox1.Checked = Properties.Settings.Default.AutoLogin;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }

        private void btnNewUser_Click(object sender, EventArgs e)
        {
            if (registerForm == null) registerForm = new RegisterForm();
            registerForm.ShowDialog();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUsername.Text == "")
            {
                MessageBox.Show("Empty Username");
                return;
            }

            Vars.UserName = txtUsername.Text;
            Vars.PassWord = txtPassword.Text;

            SaveSql save = new SaveSql();
            bool Working = save.LogIn(txtUsername.Text, txtPassword.Text);

            if (Working == true && save.State == SaveSql.LogInState.Success)
            {
                Vars.LoggedIn = true;
                this.Hide();

                Properties.Settings.Default.Username = txtUsername.Text;
                if (checkBox1.Checked)
                {
                    Properties.Settings.Default.AutoLogin = true;
                    Properties.Settings.Default.Password = txtPassword.Text;
                }
                MainForm.Instance.SetMenuInfo();
            }
            else // log in error
            {
                if (save.State == SaveSql.LogInState.ConnError)
                {
                    MessageBox.Show("Check your internet connection");
                }

                if (save.State == SaveSql.LogInState.WrongUserName)
                {
                    MessageBox.Show("Wrong Username");
                }

                if (save.State == SaveSql.LogInState.WrongPass)
                {
                    MessageBox.Show("Wrong Password");
                }

                if (save.State == SaveSql.LogInState.CorruptString)
                {
                   MessageBox.Show("The old forms was corrupted");
                   Vars.LoggedIn = true;
                   this.Hide();

                   Properties.Settings.Default.Username = txtUsername.Text;
                   if (checkBox1.Checked)
                   {
                       Properties.Settings.Default.AutoLogin = true;
                       Properties.Settings.Default.Password = txtPassword.Text;
                   }
                   MainForm.Instance.SetMenuInfo();
                   MainForm.Instance.MakeNewNote();
                }

                if (save.State == SaveSql.LogInState.OtherError)
                {
                    MessageBox.Show("An unknown error has occured: " + save.Message);
                }
            }

            Properties.Settings.Default.Save();
        }
     

        #region "Move"

        private void picLoginText_MouseDown(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = true;
            this.LastCursorPosition = new Point(e.X, e.Y);
        }

        private void picLoginText_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsMouseDown == true)
            {
                //Move the form
                this.Location = new Point(this.Left - (this.LastCursorPosition.X - e.X), this.Top - (this.LastCursorPosition.Y - e.Y));

                //Redraw the form//
                this.Invalidate();
            }
        }

        private void picLoginText_MouseUp(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = false;
        }

        private void picTopBar_MouseDown(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = true;
            this.LastCursorPosition = new Point(e.X, e.Y);
        }

        private void picTopBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsMouseDown == true)
            {
                //Move the form
                this.Location = new Point(this.Left - (this.LastCursorPosition.X - e.X), this.Top - (this.LastCursorPosition.Y - e.Y));

                //Redraw the form//
                this.Invalidate();
            }
        }

        private void picTopBar_MouseUp(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = false;
        }

        private void picLeftConer_MouseDown(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = true;
            this.LastCursorPosition = new Point(e.X, e.Y);
        }

        private void picLeftConer_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsMouseDown == true)
            {
                //Move the form
                this.Location = new Point(this.Left - (this.LastCursorPosition.X - e.X), this.Top - (this.LastCursorPosition.Y - e.Y));

                //Redraw the form//
                this.Invalidate();
            }
        }

        private void picLeftConer_MouseUp(object sender, MouseEventArgs e)
        {
            this.IsMouseDown = false;
        }

        #endregion

        #region "iconsHover"

        private void picMinimize_MouseEnter(object sender, EventArgs e)
        {
            picMinimize.Image = MinimizePicOver;
        }

        private void picMinimize_MouseLeave(object sender, EventArgs e)
        {
            picMinimize.Image = MinimizePic;
        }

        private void picClose_MouseEnter(object sender, EventArgs e)
        {
            picClose.Image = ClosePicOver;
        }

        private void picClose_MouseLeave(object sender, EventArgs e)
        {
            picClose.Image = ClosePic;
        }

        #endregion

        #region "Frame"

        private void picClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void picMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        #endregion

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            IsClosed = true;
        }

    }

}
